package com.example.pricecloudperformancetesting.utils;

import com.example.pricecloudperformancetesting.entity.QuoteDepth;
import com.example.pricecloudperformancetesting.entity.SymbolConfig;
import com.example.pricecloudperformancetesting.mapper.SymbolConfigDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.testng.annotations.Test;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author Remy
 * @description :
 * @date 2020-05-21 18:09
 */
@Component
public class DealWithSymbolQuoteDepth {

    /**
     * 读取配置文件并生成报价信息
     */
    public static List<QuoteDepth> readConfigDealWithQuoteDepth(SymbolConfig symbolConfig) {
        List<QuoteDepth> quoteDepthList = new ArrayList<>();

        //先获取深度,深度决定了生成几个对象;
        int depth = symbolConfig.getDepth();
        //价格区间
        BigDecimal priceInterval = symbolConfig.getPriceInterval();
        //价格精度
        int priceAccuracy = symbolConfig.getAccuracy();
        //volume
        Integer volume = symbolConfig.getVolume();
        for (int j = 0; j < depth; j++) {
            Double bidPrice = randomPrice(priceInterval.doubleValue(), priceAccuracy);
            Double askPrice = randomPrice(priceInterval.doubleValue(), priceAccuracy);
            Integer bidVolume = randomVolume(volume);
            Integer askVolume = randomVolume(volume);
            QuoteDepth quteDepth = QuoteDepth.builder().bidPrice(new BigDecimal(bidPrice.toString())).bidSize(bidVolume).bidOriginator(symbolConfig.getOriginator())
                    .askPrice(new BigDecimal(askPrice.toString())).askSize(askVolume).askOriginator(symbolConfig.getOriginator()).build();
            quoteDepthList.add(quteDepth);
        }

        return quoteDepthList;
    }

    /**
     * 根据配置随机产生价格
     *
     * @param:intervalPrice
     * @param:accuracy
     */
    private static double randomPrice(final double intervalPrice, int accuracy) {
        double min = intervalPrice - 1;
        double max = intervalPrice + 1;
        double price = min + ((max - min) * new Random().nextDouble());
        BigDecimal bigDecimal = new BigDecimal(price);
        return bigDecimal.setScale(accuracy, BigDecimal.ROUND_DOWN).doubleValue();
    }


    /**
     * 随机生产volume
     *
     * @param：maxVolume
     */
    private static Integer randomVolume(final Integer maxVolume) {
        int min = 0;
        return new Random().nextInt((maxVolume - min) + min);
    }

}
